//
//  ZYProportionView.m
//  YiLin
//
//  Created by Zyark on 2018/6/7.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYProportionView.h"
#import "ZYViewUtils.h"
#import "ZYImageUtils.h"

#define MARGIN self.width * 0.06

@interface ZYProportionView()
@property(nonatomic,strong) UIView *bigBgView;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIButton *confirmButton;
@property(nonatomic,strong) NSMutableArray *buttonArray;
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYProportionView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _bigBgView = [[UIView alloc] initWithFrame:self.bounds];
    _bigBgView.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self addSubview:_bigBgView];
    
    _realHeight = self.height;
    CGFloat width = self.width * 0.92;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width * 0.68)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    _bgView.centerX = self.centerX;
    _bgView.centerY = self.centerY - _bgView.height / 4;
    
    _buttonArray = [NSMutableArray arrayWithCapacity:5];
    NSArray<NSNumber *> *values = @[@0,@30,@50,@70,@100];
    for (NSUInteger i=0; i<5; i++) {
        int column = i %  3;//列
        int row = floor(i / 3);//行
        NSNumber *number = [values objectAtIndex:i];
        UIButton *button = [self setupButtonWithTitle:[NSString stringWithFormat:@"%d%%",[number intValue]]];
        button.tag = [number integerValue];
        [_bgView addSubview:button];
        if(i == 1){
            [self doButtonClick:button];
        }
        button.top = _bgView.height * 0.17  + row * (button.height + MARGIN);
        button.left = MARGIN  + column * (button.width + MARGIN);
        [_buttonArray addObject:button];
    }
    UIButton *lastButton = [_buttonArray lastObject];
    _confirmButton = [ZYViewUtils setupConfirmBtn];
    _confirmButton.size = CGSizeMake(width / 3, width * 0.12);
    [_bgView addSubview:_confirmButton];
    _confirmButton.centerX = _bgView.width / 2;
    _confirmButton.top = lastButton.bottom + MARGIN;
    [_confirmButton addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [_confirmButton setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE] forState:UIControlStateNormal];
    
    self.alpha = 0;
    self.top = -self.height;
    self.bigBgView.hidden = YES;
    self.backgroundColor = [UIColor clearColor];
}

-(UIButton *)setupButtonWithTitle:(NSString *)title{
    CGFloat width = (_bgView.width - MARGIN *4) / 3;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, 0.53 * width)];
    [_bgView addSubview:button];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = RGBA(204, 204, 204, 1).CGColor;
    button.layer.borderWidth = 1;
    [button setTitleColor:kMainColor forState:UIControlStateSelected];
    [button setTitleColor:RGBA(204, 204, 204, 1) forState:UIControlStateNormal];
    UIFont *buttonFont = [UIFont systemFontOfSize:18];
    [button.titleLabel setFont:buttonFont];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateSelected];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(void)doButtonClick:(UIButton *)button{
    for (UIButton *buttonItem in _buttonArray) {
        buttonItem.selected = NO;
        buttonItem.layer.borderColor = RGBA(204, 204, 204, 1).CGColor;
    }
    button.selected = YES;
    button.layer.borderColor = kMainColor.CGColor;
}

-(void)doConfirm:(UIButton *)button{
    NSInteger value = 0;
    for (UIButton *buttonItem in _buttonArray) {
        if(buttonItem.isSelected){
            value = buttonItem.tag;
        }
    }
    if([_delegate respondsToSelector:@selector(proportionView:doConfirm:)]){
        [_delegate proportionView:self doConfirm:value / 100];
    }
    [self close];
}

#pragma mark public
-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
        
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)setProportion:(int)proportion{
    _proportion = proportion;
    for (UIButton *buttonItem in _buttonArray) {
        if(buttonItem.tag == proportion){
            buttonItem.selected = YES;
            buttonItem.layer.borderColor = kMainColor.CGColor;
        }else{
            buttonItem.selected = NO;
            buttonItem.layer.borderColor = RGBA(204, 204, 204, 1).CGColor;
        }
    }
}

@end
